package cn.bonoon.kernel.io;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.StringValueResolver;

import cn.bonoon.kernel.io.authority.FileAuthorityHandler;

public abstract class AbstractFileManager implements FileManager, StorageInitizlier, ApplicationContextAware, EmbeddedValueResolverAware{

	protected final Logger log = LoggerFactory.getLogger(this.getClass());

	protected ApplicationContext applicationContext;
	
	protected StringValueResolver resolver;
	
	protected List<FileAuthorityHandler> authorityHandlers;

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
    }
    
	@Override
	public final void setApplicationContext(ApplicationContext context) throws BeansException {
		applicationContext = context;
	}
	
	//public abstract String getRootPath();
	
	public String resolveEmbeddedValue(String value){
		if(value.indexOf('$') != -1){
			return resolver.resolveStringValue(value);
		}
		return value;
	}
	
	@Override
	public boolean delete(StoragePersistable file) throws Exception {
		return false;
	}
	
	@Override
	public boolean load(HttpStorageFile hsFile) throws Exception {
		return internalFileLoading(hsFile);
	}
	
	/** 检查文件的权限，存储、读取或删除等， */
	@Override
	public boolean checkAuthority(HttpStorageFile hsFile) throws Exception{
		//根据类型来处理，有可能需要进行身份认证等处理
		int checked = 0;
		if(null != authorityHandlers){
			String catalog = hsFile.getCatalog();
			for(FileAuthorityHandler handler : authorityHandlers){
				if(handler.accept(catalog)){
					int result = handler.parse(hsFile);
					if(result == 2) return true;
					if(result < 0)  return false;
					checked += result;
				}
			}
		}
		
		return checked > 0;
	}
	
	/** 加载文件 */
	protected abstract boolean internalFileLoading(HttpStorageFile hsFile)throws Exception;
	
	@Override
	public FileInfo saveFile(StorageFile file) throws Exception {
		FileInfo[] fis = internalFileStored(file.init(this), 1);
		if(null != fis && fis.length > 0) return fis[0];
		return null;
	}
	
	@Override
	public FileInfo[] saveFiles(StorageFile file) throws Exception {
		return internalFileStored(file.init(this), 0);
	}
	
	/** 内部的文件存储处理，限制文件的读取个数 */
	protected abstract FileInfo[] internalFileStored(StorageFile file, int limit) throws Exception;
	
	public void setAuthorityHandlers(List<FileAuthorityHandler> authorityHandlers) {
		this.authorityHandlers = authorityHandlers;
	}
	
//	@Override
//	public boolean openFile(HttpServletRequest request, HttpServletResponse response, String fullPath) throws Exception {
//		fullPath = new String(Base64.decodeBase64(fullPath), "UTF-8");
//		HttpStorageFile hsFile = new HttpStorageFile(request, response, fullPath);
//		return internalFileLoading(hsFile);
//	}
//	
//	@Override
//	public boolean openFile(HttpServletRequest request, HttpServletResponse response, String path, String name, String ext) throws Exception {
//		HttpStorageFile hsFile = new HttpStorageFile(request, response, path, name, ext);
//		return internalFileLoading(hsFile);
//	}
//	
//	@Override
//	public boolean loadFile(HttpServletRequest request, HttpServletResponse response, String fullPath) throws Exception {
//		fullPath = new String(Base64.decodeBase64(fullPath), "UTF-8");
//		HttpStorageFile hsFile = new HttpStorageFile(request, response, fullPath);
//		hsFile.setLoadingType(HttpLoadingType.DOWNLOAD);
//		return internalFileLoading(hsFile);
//	}
//	
//	@Override
//	public boolean loadFile(HttpServletRequest request, HttpServletResponse response, String path, String name, String ext) throws Exception {
//		HttpStorageFile hsFile = new HttpStorageFile(request, response, path, name, ext);
//		hsFile.setLoadingType(HttpLoadingType.DOWNLOAD);
//		return internalFileLoading(hsFile);
//	}
	
}
